<template>
  <a-modal v-model:visible="visible" :title="id ? '编辑DTU' : '添加DTU'" :footer="null">
    <div class="py-8">
      <this-form @finished="finished" ref="formRef" />
    </div>
  </a-modal>
</template>
  <script lang="ts">
import { defineComponent, ref, nextTick } from 'vue';
import thisForm from './dtu-form.vue';

export default defineComponent({
  components: { thisForm },
  setup(props, { emit }) {
    const visible = ref<boolean>(false);
    let id = ref(0);
    let pid = ref(0);
    const formRef = ref<any>();

    const open = (form) => {
      id.value = form ? form.id : 0;
      pid.value = form ? form.pid : 0;
      visible.value = true;
      console.log(form);
      nextTick(() => {
        formRef.value.setForm(form);
      });
    };

    const finished = (event) => {
      visible.value = false;
      if (id.value) {
        return emit('edited', {
          id: id.value,
          ...event,
        });
      }
      return emit('added', {
        id: id.value,
        pid:pid.value,
        ...event,
      });
    };

    return {
      visible,
      finished,
      open,
      id,
      formRef,
    };
  },
});
</script>
  
